<template>
  <card v-bind="cardOptions" @save="save">
    <y-form ref="$form" v-bind="options2"></y-form>
  </card>
</template>

<script>
export default {
  mounted() {
    var { keyValue } = this.$route.query;
    if (!!keyValue) {
      this.cardOptions.title = "商品属性 / 编辑";
      this.initData(keyValue);
    } else {
      this.cardOptions.title = "商品属性 / 新增";
    }
  },
  data() {
    return {
      keyValue: "",
      cardOptions: {
        title: "",
        btns: ["save", "back"],
      },
      options2: {
        items: [
          {
            title: "产品信息",
            fields: [
              { type: "input", field: "code", text: "编号" ,required: true },
              { type: "input", field: "name", text: "名称", required: true },

            ],
          },
          {
            title:"属性选项",
            field:"items",
            height: "300px",
            edit: true,
            columns: [
              { type: "seq", width: 50 },
              { field: "name", title: "名称" },
            ],
          }
        ],
      },
    };
  },
  methods: {
    async initData(keyValue) {
      var { data } = await $y.api.shop.attribute.get(keyValue);
      this.$refs.$form.setData(data);
    },
    async save() {
      var formData = this.$refs.$form.getFormData();
      if(!formData.id){
        debugger
        var { succeeded } = await this.$y.api.shop.attribute.insert([formData])
        if(succeeded){
          $y.notifier.success("新增成功！");
        }
      }else{
        var { succeeded } = await this.$y.api.shop.attribute.update([formData])
        if(succeeded){
          $y.notifier.success("保存成功！");
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
</style>